import http from './http.js'
import md5 from 'js-md5'

// 登录
export function login(account,psw) {
    return http.post('user/login', {
        loginName:account,
        passwordMd5:md5(psw)
    }).then(res => {
        return res
    })
}

// 注册
export function register(account, psw) {
    return http.post('user/register', {
        loginName: account,
        password: psw
    }).then(res => {
        return res
    })
}

// 获取首页数据
export function homeData() {
    return http.get('/index-infos').then(res => {
        return res
    })
}

// 获取商品详情页的数据
export function getGoodsDetailData(id) {
    return http.get('goods/detail/'+id)
}

// 加入购物车
export function addGoodsToCart(info) {
    return http.post('/shop-cart',info)
}

// 获取购物车中的商品信息
export function getCartData() {
    return http.get('/shop-cart')
}

// 删除购物车商品
export function deleGoods(id) {
    return http.delete('/shop-cart/'+id)
}

// 增减购物车中的商品数量
export function changeGoodsNum(data) {
    return http.put('/shop-cart/',data)
}



// ======================= 获取地址列表
export function getAddressList() {
    return http.get("/address")
}

// ======================= 根据ID获取某个地址，实现数据回显
export function getAddressDetail(id) {
    return http.get("/address/" + id)
}

// ======================= 编辑地址
export function updateAddress(obj) {
    return http.put("/address", obj)
}

// ======================= 新增地址
export function addAddress(obj) {
    return http.post("/address", obj)
}

// ======================= 删除地址
export function removeAddress(id) {
    return http.delete("/address/" + id)
}



// =========================生成订单页面拿到要结算的商品数据
export function getCheckedGoods(id) {
    return http.get('/shop-cart/settle?cartItemIds='+id)
}

// =======================点击生成订单时保存订单
export function saveOrder(addressId,cartItemIds) {
    return http.post('/saveOrder', {
        addressId: addressId,
        cartItemIds:cartItemIds,
    })
}

//================ 选择支付方式后支付成功
export function paySuccess(orderNo,payType) {
    return http.get(`paySuccess?orderNo=${orderNo}&payType=${payType}`)
}

// ===============我的订单页面获取订单数据
export function getOrderData(pageNumber,status) {
    return http.get(`/order?pageNumber=${pageNumber}&status=${status}`)
}

// ================我的页面获取用户信息
export function getUserInfo() {
    return http.get('/user/info')
}

// ================我的页面保存修改后的数据
export function saveEditedData(introduceSign, nickName, psw) {
    return http.put('/user/info', {
        introduceSign: introduceSign,
        nickName: nickName,
        passwordMd5:md5(psw)
    })
}


// ====================订单详情页
export function getOrderDetailData(orderNo) {
    return http.get('/order/'+orderNo)
}

// ===================确认收货
export function confirmGoods(orderNo) {
    return http.put(`/order/${orderNo}/finish`)
}

// ============取消订单
export function cancelOrder(orderNo) {
    return http.put(`order/${orderNo}/cancel`)
}

// =============退出登录
export function loginOut() {
    return http.get('/index-infos')
}